﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Pic : System.Web.UI.Page
{
    protected void Add_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            if (!ManagerLogin.ValidateRole(string.Format("2_{0}_c", this.PicClassIdList.SelectedValue)))
            {
                this.Msg.CssClass = "errorMsg";
                this.Msg.Text = "您无此权限";
                this.Msg.Visible = true;
            }
            else
            {
                string str = (string)this.Session["MenuId"];
                string text1 = (string)this.Session["FileName"];
                string text2 = (string)this.Session["MenuName"];
                if (str == null)
                {
                    ManagerLogin.Out();
                }
                else
                {
                    string str2 = str;
                    string path = string.Format("{0}{1}/Small/{2}", FSO.GetUploadPath(), str2, FSO.RandomFileName(this.PicSmall.FileName));//
                    //string str4 = string.Format("{0}{1}/Big/{2}", FSO.GetUploadPath(), str2, FSO.RandomFileName(this.PicBig.FileName));

                    string filePath = base.Server.MapPath(path);//生成的图片
                    //string str6 = base.Server.MapPath(str4);//原图
                    FSO.PathExists(filePath);//查找目录，没有就创建
                    //FSO.PathExists(str6);
                    this.PicSmall.SaveAs(filePath);
                    //this.PicBig.SaveAs(str6);//上传原图
                    //WebPublic.WebPublic.MakeThumbnail(str6, filePath, 130, 130, "Cut");//生成小图

                    //生成单页的名称
                    string addtime = DateTime.Now.ToString();
                    string Direc = "../html/" + DateTime.Now.Year + DateTime.Now.Month + "/";
                    string htmladdress = Direc + addtime.Replace("-", "").Replace(" ", "").Replace(":", "").Replace("/", "") + ".html";

                    string[,] includeColumns = new string[,] {
                        { "PicPathSmall", path, "NvarChar", "0" }, 
                        { "PicPathBig", path, "NVarChar", "0" }, 
                        { "PicClassId", this.PicClassIdList.SelectedValue.ToString(), "int", "0" }, 
                        { "Title", this.PicTitle.Text, "NVarChar", "0" }, 
                        { "PicShortContent", this.PicShortContent.Text, "NVarChar", "0" }, 
                        { "PicContent", this.PicContent.Text.ToString(), "NText", "0" }, 
                        { "MenuId", (string)this.Session["MenuId"], "int", "0" }, 
                        { "AddTime", this.AddTime.Text, "DateTime", "0" }, 
                        { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" },
                        { "htmladdress", htmladdress,"NVarChar","0" },
                        { "Weight", this.txtorder.Text,"int","32" },
                        { "keywords", txtkeys.Text.ToString(), "NVarChar", "0" }
                    };

                    IDb db = new DbFactory().DriverDb();
                    db.Open();
                    db.Insert("Pic", includeColumns);
                    db.Close();
                    this.Msg.Text = "图文添加成功！";
                    this.Msg.Visible = true;
                    this.PicTitle.Text = "";
                    this.PicShortContent.Text = "";
                    this.PicContent.Text = "";
                    this.AddTime.Text = DateTime.Now.ToString();
                }
            }
        }
    }

    protected void Bind()
    {
        string str = base.Request.QueryString["menuId"];
        if (str == null)
        {
            string str2 = (string)this.Session["MenuId"];
            string text1 = (string)this.Session["FileName"];
            string text2 = (string)this.Session["MenuName"];
            if (str2 == null)
            {
                ManagerLogin.Out();
                return;
            }
            str = str2;
        }
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataRow row = db.TopRow("MenuName,FileName,MenuId", "menu", string.Format("MenuId={0}", str), "");
        if (db.Scalar("count(PicClassId)", "PicClass", string.Format("MenuId={0}", str)) < 1)
        {
            string[,] includeColumns = new string[,] { { "ClassName", (string)row["MenuName"], "NVarChar", "0" }, { "FileName", (string)row["FileName"], "NVarChar", "32" }, { "MenuId", str, "int", "0" }, { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } };
            db.Insert("PicClass", includeColumns);
        }
        DataTable oldTable = db.DataTable("ClassName,ParentId,PicClassId", "PicClass", string.Format("MenuId={0}", str), "OrderId ASC");
        db.Close();
        oldTable.Columns.Add("ClassNameText");
        DataTable newTable = oldTable.Clone();
        Function.PopulateTable(oldTable, newTable, "ClassName", "PicClassId", "ParentId", 0, 0);
        this.PicClassIdList.DataSource = newTable;
        this.PicClassIdList.DataTextField = "ClassNameText";
        this.PicClassIdList.DataValueField = "PicClassId";
        this.PicClassIdList.DataBind();
        string str3 = base.Request.QueryString["PicClassId"];
        if (str3 != null)
        {
            this.PicClassIdList.SelectedValue = str3;
        }
        this.AddTime.Text = DateTime.Now.ToString();
        this.Session["MenuName"] = (string)row["MenuName"];
        this.Session["FileName"] = (string)row["FileName"];
        this.Session["MenuId"] = (string)row["MenuId"];
        this.MenuName.Text = (string)row["MenuName"];
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            Convert.ToDateTime(this.AddTime.Text);
            args.IsValid = true;
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }
    

    protected void PicSmallCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.PicSmall.HasFile)
        {
            string extension = FSO.GetExtension(this.PicSmall.FileName);
            string extensionList = ".jpg|.gif|.png|.bmp";
            args.IsValid = FSO.ExtensionValid(extension, extensionList);
            this.PicSmallCustomValidator.ErrorMessage = "小图格式错误，合法格式：" + extensionList;
        }
        else
        {
            this.PicSmallCustomValidator.ErrorMessage = "小图路径选择错误！";
            args.IsValid = false;
        }
    }
}
